package mysql

import (
	"login-mgr/loginrpc/loginrpc"

	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	GoodsName  string  `gorm:"type:varchar(30);not null;comment:'商品名称'"`
	GoodsPrice float32 `gorm:"type:decimal(10,2);not null;comment:'商品价格'"`
	GoodsCode  string  `gorm:"type:varchar(100);not null;comment:'商品编号'"`
	Name       string  `gorm:"type:varchar(30);not null;comment:'供货商'"`
	UserId     int64   `gorm:"type:int(11);not null;comment:'用户ID'"`
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func (g *Goods) UserGoodsList(db *gorm.DB, id *loginrpc.GoodsListRequest, lists []*loginrpc.GoodsList) []*loginrpc.GoodsList {
	/*
		SELECT
		  users.id as user_id,
		  phone,
		  goods.goods_name,
		  goods.goods_price,
		  goods.goods_code,
		  name
		FROM
		users
		LEFT JOIN goods ON users.id=goods.user_id
	*/
	db.Debug().Table("users").
		Select(" users.id as user_id,  goods.goods_name,  goods.goods_price, goods.goods_code, name").
		Joins("LEFT JOIN goods ON users.id=goods.user_id").
		Where("users.id=?", id).Find(&lists)
	return lists
}
